<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>即时聊天 - 管理员面板</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }
        .header {
            background-color: #4CAF50;
            color: white;
            padding: 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            font-size: 20px;
        }
        .header .user-info {
            display: flex;
            align-items: center;
        }
        .header .user-name {
            margin-right: 15px;
        }
        .header .back-btn {
            background-color: #2196F3;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            text-decoration: none;
        }
        .header .back-btn:hover {
            background-color: #0b7dda;
        }
        .header .logout-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .admin-section {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .error {
            background-color: #ffebee;
            color: #c62828;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 20px;
            border-left: 4px solid #c62828;
        }
        .success {
            background-color: #e8f5e9;
            color: #2e7d32;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 20px;
            border-left: 4px solid #2e7d32;
        }
        /* 添加用户表单 */
        .add-user-form {
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
            font-weight: bold;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        .form-group.checkbox {
            display: flex;
            align-items: center;
        }
        .form-group.checkbox input {
            width: auto;
            margin-right: 10px;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #45a049;
        }
        /* 用户列表 */
        .users-table {
            width: 100%;
            border-collapse: collapse;
        }
        .users-table th,
        .users-table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        .users-table th {
            background-color: #f5f5f5;
            font-weight: bold;
            color: #333;
        }
        .users-table tr:hover {
            background-color: #f9f9f9;
        }
        .btn-danger {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-danger:hover {
            background-color: #d32f2f;
        }
        .btn-danger:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 10px;
            font-size: 12px;
            font-weight: bold;
        }
        .badge-admin {
            background-color: #ff9800;
            color: white;
        }
        .badge-user {
            background-color: #2196F3;
            color: white;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>管理员面板</h1>
        <div class="user-info">
            <span class="user-name">管理员, <%= user.username %></span>
            <a href="/" class="back-btn">返回聊天</a>
            <form action="/logout" method="POST" style="display: inline;">
                <button type="submit" class="logout-btn">退出登录</button>
            </form>
        </div>
    </div>
    
    <div class="container">
        <div class="admin-section">
            <h2>用户管理</h2>
            
            <% if (typeof error !== 'undefined' && error) { %>
                <div class="error"><%= error %></div>
            <% } %>
            
            <!-- 添加用户表单 -->
            <div class="add-user-form">
                <h3>添加新用户</h3>
                <form action="/admin/add-user" method="POST">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username" required>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" id="password" name="password" required>
                    </div>
                    <div class="form-group checkbox">
                        <input type="checkbox" id="is_admin" name="is_admin">
                        <label for="is_admin">设置为管理员</label>
                    </div>
                    <button type="submit" class="btn">添加用户</button>
                </form>
            </div>
            
            <!-- 用户列表 -->
            <h3>用户列表</h3>
            <table class="users-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>角色</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% users.forEach(function(user) { %>
                        <tr>
                            <td><%= user.id %></td>
                            <td><%= user.username %></td>
                            <td>
                                <span class="badge <%= user.is_admin ? 'badge-admin' : 'badge-user' %>">
                                    <%= user.is_admin ? '管理员' : '普通用户' %>
                                </span>
                            </td>
                            <td>
                                <form action="/admin/delete-user/<%= user.id %>" method="POST" onsubmit="return confirm('确定要删除用户 <%= user.username %> 吗？');">
                                    <button type="submit" class="btn-danger" <%= user.id == currentUser.id ? 'disabled' : '' %>>
                                        删除
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <% }); %>
                </tbody>
            </table>
        </div>
    </div>
    
    <script>
        // 将当前用户ID存储到JavaScript中，以便在模板中使用
        const currentUser = { id: <%= user.id %> };
    </script>
</body>
</html>